package com.xzx.flink.wordcount;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

/**
 * wc
 *
 * @author xinzhixuan
 * @version 1.0
 * @date 2021-08-21 9:38 下午
 */
public class BatchWordCountV2 {
    public static void main(String[] args) throws Exception {
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        env.readTextFile("/Users/xinzhixuan/work/git/flink-study-java/src/main/resources/wordcount.txt")
                .flatMap((String value, Collector<Tuple2<String, Integer>> out) -> {
                    String[] words = value.split(" ");
                    for (String word : words) {
                        out.collect(Tuple2.of(word, 1));
                    }
                })
                .returns(Types.TUPLE(Types.STRING, Types.INT))// 因为lambda表达类型不够，所以需要额外的types来指定类型
                .groupBy(0).sum(1).print();
    }
}
